package com.test.field;

import java.io.IOException;

import javax.microedition.khronos.opengles.GL;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.test.drawing.OpenGLRenderer;
import com.test.drawing.StaticImage;
import com.test.drawing.Player.Team;
import com.test.drawing.PlayerModel;
import com.test.loading.ModelLoadManager;
import com.test.positioning.MatrixTrackingGL;
import com.test.positioning.Projector;


public class ModelView extends GLSurfaceView {

    private OpenGLRenderer glRender;
//    private List<SoccerPlayer> playersListA, playersListB;
    
    private PlayerModel player;

	ModelView(Context context) {
		super(context);
	}

	public ModelView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void init() {
		// Accelerometro
		setFocusableInTouchMode(true);
		setGLWrapper(new GLSurfaceView.GLWrapper() {
			public GL wrap(GL gl) {
				return new MatrixTrackingGL(gl);
			}
		});
		
		// Xoom resolution
		glRender = new OpenGLRenderer(1280, 800);
		
		setEGLConfigChooser(8, 8, 8, 8, 16, 0);

		setRenderer(glRender);
		
//		if (!ModelLoadManager.isSceneLoaded())
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		
		player = new PlayerModel(ModelLoadManager.createAnimation(Team.First_Team));
		player.setBoard(ModelLoadManager.createBoard());
		
		player.setBoardNumber(10);
//		player.setPositionInPixels(400, 400);
		player.setZ(-60);
		
		glRender.addDrawable(player.getPlayerMesh(), 0);
		glRender.addDrawable(player.getPlayerBoard(), 1);
		
		Projector.getInstance().addPositionChanger(player);
		
		
		
		StaticImage si = new StaticImage(0, 0, 1280, 700, Projector.getInstance());
		
		try {
			si.loadBitmap(
					BitmapFactory.decodeStream(getContext().getAssets().open("standard_field.png")));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		glRender.addDrawable(si, 0);
		
		
//		playersA = new ArrayList<Player>();
//		playersB = new ArrayList<Player>();
//
//		// Create only after SoccerPlayer Lists are Setted
//		createModels();
	}
	

	
	private boolean touchPressed = false;
	
	public boolean onTouchEvent(final MotionEvent event) {

		if (event.getAction() == MotionEvent.ACTION_DOWN && touchPressed) {
			return true;
		}
		
		if (event.getAction() == MotionEvent.ACTION_UP && touchPressed) {
			touchPressed = false;
			return true;
		}
		if (touchPressed) return true;
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			touchPressed = true;
		}
		
		float x = event.getX();
		float y = event.getY();
		
		player.setPositionInPixels(x, y);
		
//		
//		// First Team - Run
//		for (int i = 0 ; i < playersA.size() ; i++) {
//			Player player = playersA.get(i);
//			if (player.isTouched(x, y)) {
//				
//				playerTouched = true;
//				touchedPlayer = player;
//				player.getModel().animate();
//				
//				System.out.println("Anima Jogador" + playersB.get(i).getNumber());
//				
//			} else {
//
//				player.getModel().stopAnimate();
//				
//			}
//		}
//		
//		// Second Team - Run
//		for (int i = 0 ; i < playersB.size() ; i++) {
//			Player player = playersB.get(i);
//			if (player.isTouched(x, y)) {
//				
//				playerTouched = true;
//				touchedPlayer = player;
//				player.getModel().animate();
//				
//			} else {
//				
//				player.getModel().stopAnimate();
//				
//			}
//		}
//		
//		if (!playerTouched) touchedPlayer = null;
//		
//		notifyListeners();
		
		return true;
	}

	@Override
	public void finalize() {
		try {
			super.finalize();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}
